﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Myproject.DTO.relationship;
using Myproject.DataAccess.factory;
using Myproject.DataCRM;
using Myproject.Enum;

namespace Myproject.DataAccess.entity
{
    public class UserRoleProcess : EntityProcess
    {
        public void addUserRole(UserRole TUserRole, Enum.transactionEnum TransactionInfo)
        {
            using (DBFactoryInstance dbInstance = DBFactoryInstance.DbInstance)
            {
                switch(TransactionInfo)
                {
                    case transactionEnum.StartToEnd:
                    case transactionEnum.End:
                    {
                        AddUserRoleFunction(TUserRole, dbInstance);
                        TransactionInstance.Complete();
                        TransactionInstance.Dispose();
                        break;
                    }
                    case transactionEnum.Start:
                    case transactionEnum.Running:
                    {
                        AddUserRoleFunction(TUserRole, dbInstance);
                        break;
                    }
                }
            }
        }

        private void AddUserRoleFunction(UserRole TUserRole, DBFactoryInstance dbInstance)
        {
            if (TransactionInstance == null) TransactionInstance = new System.Transactions.TransactionScope();
            dbInstance.AddToT_USER_ROLE(TUserRole);
            dbInstance.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
        }

        public void updateUserRole(UserRole TUserRole, Enum.transactionEnum TransactionInfo)
        {
            using (DBFactoryInstance dbInstance = DBFactoryInstance.DbInstance)
            {
                switch(TransactionInfo)
                {
                    case transactionEnum.StartToEnd:
                    case transactionEnum.End:
                    {
                        UpdateUserRoleFunction(TUserRole, dbInstance);
                        TransactionInstance.Complete();
                        TransactionInstance.Dispose();
                        break;
                    }
                    case transactionEnum.Start:
                    case transactionEnum.Running:
                    {
                        UpdateUserRoleFunction(TUserRole, dbInstance);
                        break;
                    }
                }
            }
        }

        private void UpdateUserRoleFunction(UserRole TUserRole, DBFactoryInstance dbInstance)
        {
            if (TransactionInstance == null) TransactionInstance = new System.Transactions.TransactionScope();
            T_USER_ROLE userInstance = dbInstance.T_USER_ROLE.SingleOrDefault(c => c.id == TUserRole.id);
            userInstance.userid = TUserRole.userid;
            userInstance.roldid = TUserRole.roldid;
            userInstance.status = TUserRole.status;
            userInstance.updatetime = DateTime.Now;
            userInstance.updater = TUserRole.updater;
            dbInstance.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
        }

        public void deleteUserRole(UserRole TUserRole, Enum.transactionEnum TransactionInfo)
        {
            using (DBFactoryInstance dbInstance = DBFactoryInstance.DbInstance)
            {
                switch(TransactionInfo)
                {
                    case transactionEnum.StartToEnd:
                    case transactionEnum.End:
                    {
                        DeleteUserRoleFunction(TUserRole, dbInstance);
                        TransactionInstance.Complete();
                        TransactionInstance.Dispose();
                        break;
                    }
                    case transactionEnum.Start:
                    case transactionEnum.Running:
                    {
                        DeleteUserRoleFunction(TUserRole, dbInstance);
                        break;
                    }
                }
            }
        }

        private void DeleteUserRoleFunction(UserRole TUserRole, DBFactoryInstance dbInstance)
        {
            if (TransactionInstance == null) TransactionInstance = new System.Transactions.TransactionScope();
            dbInstance.DeleteObject(dbInstance.T_USER_ROLE.SingleOrDefault(c => c.id == TUserRole.id));
            dbInstance.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
        }

        public List<UserRole> GetUserRoleListByUserRole(UserRole TUserRole)
        {
            List<UserRole> result = new List<UserRole>();

            using (DBFactoryInstance dbInstance = DBFactoryInstance.DbInstance)
            {
                var list = from u in dbInstance.T_USER_ROLE
                           where (TUserRole.id == null || TUserRole.userid == u.id) &&
                                 (TUserRole.userid == null || TUserRole.userid == u.userid) &&
                                 (TUserRole.roldid == null || TUserRole.roldid == u.roldid) &&
                                 (TUserRole.status == null || TUserRole.status == u.status) &&
                                 (TUserRole.StartCreateTime == null || TUserRole.StartCreateTime <= u.createtime) &&
                                 (TUserRole.EndCreateTime == null || TUserRole.EndCreateTime >= u.createtime) &&
                                 (TUserRole.StartUpdateTime == null || TUserRole.StartUpdateTime <= u.updatetime) &&
                                 (TUserRole.EndUpdateTime == null || TUserRole.EndUpdateTime >= u.updatetime)
                           select u;
                foreach (var listvalue in list)
                {
                    result.Add(new UserRole
                    {
                        id = listvalue.id,
                        userid = listvalue.userid,
                        //T_USER = listvalue.T_USER,/***包装之后的实体，不可以做延迟加载，因为在自动生成的Model里面，有自动化匹配校验,实现此方面，需要在业务层，通过UserId调取TUser的Entity***/
                        roldid = listvalue.roldid,
                        //T_ROLE = listvalue.T_ROLE,/***包装之后的实体，不可以做延迟加载，因为在自动生成的Model里面，有自动化匹配校验,实现此方面，需要在业务层，通过RoleID调取TRole的Entity***/
                        status = listvalue.status,
                        createtime = listvalue.createtime,
                        updatetime = listvalue.updatetime,
                        author = listvalue.author,
                        updater = listvalue.updater
                    });
                }
            }

            return result;
        }

    }
}
